﻿using System;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_OrgPersTree_Right : System.Web.UI.Page
{
    DeptPersManage MyDPM = new DeptPersManage();
    CommClass MyCL = new CommClass();
    public int type = 0;
    public static string sSPersName = "", sSCardID = "";
    public string sOrgCode = "", sOrgType = "";
    public int AllCount = 0, AllPageCount = 0, NowPageCount = 0, PageSize = 0;
    DataTable  MyDV;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            Response.Redirect("../ReLogon.aspx");
        }
        try
        {
            sOrgCode = Request.QueryString["OrgCode"].ToString();
        }
        catch
        {
            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('参数传递错误!');</script>");
        }
        try
        {
            //sOrgType = Request.QueryString["OrgType"].ToString();
            HOrgType.Value = Request.QueryString["OrgType"].ToString();
        }
        catch
        {
            HOrgType .Value = "0";
            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('参数传递错误!');</script>");
        }
        if (sOrgCode == "" || sOrgCode == null)
        {
            sOrgCode = "0";
        }

        if (!Page.IsPostBack)
        {
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue"); 
            if (ViewState["VPersName"] == null || ViewState["VPersName"].ToString().CompareTo("") == 0)
            {
                ViewState["VPersName"] = "";
            }
            if (ViewState["VCardID"] == null || ViewState["VCardID"].ToString().CompareTo("") == 0)
            {
                ViewState["VCardID"] = "";
            }

            if (ViewState["OpType"] == null || ViewState["OpType"].ToString().CompareTo("") == 0)
            {
                ViewState["OpType"] = "1";
            }
            if (ViewState["SortExpression"] == null || ViewState["SortExpression"].ToString().CompareTo("") == 0)
            {
                ViewState["SortExpression"] = "ID";
            }

            if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
            {
                ViewState["SortDirection"] = " ";
            }
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
        }
    }

    private void Bind(string sSqlSort)
    {
        if (ViewState["OpType"].ToString() == "0")  //初始状态
        {
            
        }
        else if (ViewState["OpType"].ToString() == "1")  //查询状态
        {
            MyDV = MyDPM .SearchOrgPersInfo (ViewState["VPersName"].ToString().Trim(), ViewState["VCardID"].ToString().Trim(), sOrgCode, "1", sSqlSort);
        }
        else if (ViewState["OpType"].ToString() == "2") // 显示删除
        {
            MyDV = MyDPM.SearchOrgPersInfo(ViewState["VPersName"].ToString().Trim(), ViewState["VCardID"].ToString().Trim(), sOrgCode, "0,1", sSqlSort);
        }
        GridView1.DataSource = MyDV;
        GridView1.DataBind();
        AllPageCount = GridView1.PageCount;
        NowPageCount = GridView1.PageIndex + 1;
        AllCount = MyDV.Rows.Count;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            if (e.Row.RowIndex % 2 != 0)
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text.ToString().Trim() + "|" + e.Row.Cells[1].Text.ToString().Trim() + "|" + e.Row.Cells[2].Text.ToString().Trim() + "|" + e.Row.Cells[3].Text.ToString().Trim() + "|" + e.Row.Cells[4].Text.ToString().Trim() + "|" + e.Row.Cells[5].Text.ToString().Trim() + "|" + e.Row.Cells[6].Text.ToString().Trim() + "|" + e.Row.Cells[7].Text.ToString().Trim() + "|" + e.Row.Cells[8].Text.ToString().Trim() + "|" + e.Row.Cells[9].Text.ToString().Trim() + "|" + e.Row.Cells[10].Text.ToString().Trim() + "|" + e.Row.Cells[11].Text.ToString().Trim() + "|" + e.Row.Cells[12].Text.ToString().Trim() + "|" + e.Row.Cells[13].Text.ToString().Trim() + "|" + e.Row.Cells[14].Text.ToString().Trim() + "|" + e.Row.Cells[15].Text.ToString().Trim() + "|" + e.Row.Cells[16].Text.ToString().Trim() + "|" + e.Row.Cells[17].Text.ToString().Trim() + "|" + e.Row.Cells[18].Text.ToString().Trim() + "|" + e.Row.Cells[19].Text.ToString().Trim() + "'),tog(this,'#F9F9FB')");
            }
            else
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text + "|" + e.Row.Cells[1].Text + "|" + e.Row.Cells[2].Text + "|" + e.Row.Cells[3].Text + "|" + e.Row.Cells[4].Text + "|" + e.Row.Cells[5].Text + "|" + e.Row.Cells[6].Text + "|" + e.Row.Cells[7].Text + "|" + e.Row.Cells[8].Text + "|" + e.Row.Cells[9].Text + "|" + e.Row.Cells[10].Text + "|" + e.Row.Cells[11].Text + "|" + e.Row.Cells[12].Text + "|" + e.Row.Cells[13].Text + "|" + e.Row.Cells[14].Text + "|" + e.Row.Cells[15].Text + "|" + e.Row.Cells[16].Text + "|" + e.Row.Cells[17].Text + "|" + e.Row.Cells[18].Text + "|" + e.Row.Cells[19].Text + "'),tog(this,'#F9F9FB')");
            }
            e.Row.Cells[5].Text = e.Row.Cells[5].Text.Trim();
            if (e.Row.Cells[18].Text == "0")
                e.Row.ForeColor = Color.Red;
        }
    }
    protected void SearchPersInfo_ServerClick(object sender, EventArgs e)
    {
        ViewState["VPersName"] = MyCL.CutLimitString(PersName.Text.ToString().Trim()); //去掉特殊字符
        ViewState["VCardID"] = MyCL.CutLimitString(CardID.Text.ToString().Trim());
        ViewState["OpType"]    = "1";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnExport_Click(object sender, EventArgs e)
    {

    }
    protected void BtnDel_Click(object sender, EventArgs e)
    {
        string sOrgPersID = HOrgPersID.Value.ToString().Trim();
        string sPersID = HPersID.Value.ToString().Trim();
        string sOrgID = HorgID.Value.ToString().Trim();
        int iBack = MyDPM .IFDelOrgPersInfo (sOrgPersID, "0");
        if (iBack == 1)
        {
            MyDPM.UpOrgPersNumber(sOrgID, sOrgID);     
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('标记删除成功!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
            return;
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('标记删除失败!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
            return;
        }
    }
    protected void BtnDispDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "2";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnHideDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "1";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnResume_Click(object sender, EventArgs e)
    {
        string sOrgPersID = HOrgPersID.Value.ToString().Trim();
        string sPersID = HPersID.Value.ToString().Trim();
        string sOrgID = HorgID.Value.ToString().Trim();
        bool isCanResume = HEffective.Value == "1" ? false : true;
        if (!isCanResume)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('没有被删除的人员,不能执行恢复!');</script>");
            return;
        }
        int iBack = MyDPM.IFDelOrgPersInfo(sOrgPersID, "1");

        if (iBack == 1)
        {
             MyDPM.UpOrgPersNumber(sOrgID, sOrgID);
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复成功!');</script>");
            ViewState["OpType"] = "2";
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
            return;
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复失败!');</script>");
            return;
        }
    }
    protected void BtnFB_Click(object sender, EventArgs e)
    {
        try
        {
            //MYUM.sys_DeptPersRelease(HOrgType.Value.ToString().Trim(), "0");
           int iBack = MyDPM .Sys_Pers_Send (HOrgType.Value.ToString().Trim(), "0").Rows.Count ;
           if (iBack >0)
           {
               ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('发布成功!');location.href='OrgPersTree_Right.aspx';</script>");
               return;
           }
           else
           {
               ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('发布失败或者没有可发布的数据!');</script>");
               return;
           }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('发布失败!');</script>");
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"].ToString() == "desc")
        {
            ViewState["SortDirection"] = " ";
        }
        else
        {
            ViewState["SortDirection"] = "desc";
        }

        ViewState["SortExpression"] = e.SortExpression;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);

    }
    protected void BtnYJDel_Click(object sender, EventArgs e)
    {
        string sOrgPersID = HOrgPersID.Value.ToString().Trim();
        string sPersID = HPersID.Value.ToString().Trim();
        string sOrgID = HorgID.Value.ToString().Trim();
        HsTec.Mobile.Green.BLL.DeptPersManager dpm = new HsTec.Mobile.Green.BLL.DeptPersManager();
        int iBack = dpm.delPersonInfo(sOrgPersID, sPersID);
        if (iBack == 1)
        {
            MyDPM.UpOrgPersNumber(sOrgID, sOrgID);
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('永久删除成功!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
            return;
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('永久删除失败!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
            return;
        }
    }
}
